﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VK.Casemeet.Employees;
using VK.Comm;

namespace VK.Casemeet.Web.Admin.Employees
{
    public partial class RoleAdd : System.Web.UI.Page
    {
        public int area1, area2, group = 1;

        protected void Page_Load(object sender, EventArgs e)
        {
            (this.Master as Site).SiteMapPathControl.MapPathList = new List<MapPath>(){
                new MapPath{ Text="桌面",Url="/Admin/Home.aspx"},
                new MapPath{ Text="角色列表",Url="/Admin/Employees/RoleList.aspx"},
                new MapPath{ Text="新建角色",Url="#"}
            };

            if (!IsPostBack)
            {
                LoadData();
            }
        }

        internal void LoadData()
        {
            rptRights.DataSource = RoleBLL.GetRightsList();
            rptRights.DataBind();
        }

        protected bool IsHasRights(List<RoleRightsEntity> roleRightslst, int rightsID)
        {
            bool results = false;
            for (int n = 0; n < roleRightslst.Count(); n++)
            {
                if (roleRightslst[n].RightsID == rightsID)
                {
                    results = true;
                    break;
                }
            }
            return results;
        }

        protected void butSave_Click(object sender, EventArgs e)
        {

            RoleEntity role = LoadEntity();

            if (RoleBLL.InsertRole(role) > 0)
            {
                Message.Msg(this.Page, CS.MsgAddSuccess, MessageType.Success);
                Util.ClearControl((this.Master as Site).contentBody.Controls);
            }
            else
            {
                Message.Msg(this.Page, CS.MsgAddFailure, MessageType.Failure);
            }
        }

        protected RoleEntity LoadEntity()
        {
            RoleEntity role = new RoleEntity();
            role.Name = txtName.Text;
            role.ProvinceID = Util.GetFormInt("ddlArea1");
            role.CityID = Util.GetFormInt("ddlArea2");
            role.RoleRightslst = new List<RoleRightsEntity>();
            string rights = Util.GetFormString("chkRights");
            if (!string.IsNullOrEmpty(rights))
            {
                string[] rightsArray = rights.Split(new char[1] { ',' });
                for (int n = 0; n < rightsArray.Length; n++)
                {
                    RoleRightsEntity roleRights = new RoleRightsEntity();
                    roleRights.RightsID = int.Parse(rightsArray[n]);
                    role.RoleRightslst.Add(roleRights);
                }
            }
            return role;
        }
    }
}